package org.example;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MessageProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    
    // 发送普通消息
    public void sendMessage(String topic, String message) {
        rocketMQTemplate.convertAndSend(topic, message);
        log.info("发送消息成功: topic={}, message={}", topic, message);
    }
    
    // 发送订单消息
    public void sendOrderMessage(String topic, Order order) {
        Message<Order> message = MessageBuilder.withPayload(order).build();
        rocketMQTemplate.send(topic, message);
        log.info("发送订单消息成功: topic={}, order={}", topic, order);
    }
    
    // 发送延迟消息（延迟级别：1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h）
    public void sendDelayMessage(String topic, String message, int delayLevel) {
        rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload(message).build(), 
                                 rocketMQTemplate.getProducer().getSendMsgTimeout(), 
                                 delayLevel);
        log.info("发送延迟消息成功: topic={}, message={}, delayLevel={}", topic, message, delayLevel);
    }
    
    // 发送事务消息（简化版）
    public void sendTransactionMessage(String topic, String message) {
        rocketMQTemplate.sendMessageInTransaction(topic, 
            MessageBuilder.withPayload(message).build(), null);
        log.info("发送事务消息: topic={}, message={}", topic, message);
    }
}